package com.thingcom.delinrobot.main.device;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import com.thingcom.delinrobot.Common.LanguageType;
import com.thingcom.delinrobot.DelinApplication;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.main.Launch.LaunchActivity;
import com.thingcom.delinrobot.utils.LanguageUtil;
import com.thingcom.delinrobot.utils.SpUtil;
import com.thingcom.delinrobot.utils.ToastUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/11/21
 */
public class ChangeLanguageBackFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_change_language)
    SimpleToolbar toolbarChangeLanguage;
    @BindView(R.id.wheel_change_language)
    WheelView wheelChangeLanguage;
    @BindView(R.id.btn_set_data)
    Button btnSetData;
    private int choose;

    public static ChangeLanguageBackFragment newInstance() {
        Bundle bundle = new Bundle();
        ChangeLanguageBackFragment fragment = new ChangeLanguageBackFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_language_change, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar(toolbarChangeLanguage);
        initView();
        return view;
    }

    private void initView() {
        wheelChangeLanguage.setCyclic(true);

        final List<String> mLanguageItems = new ArrayList<>();
        mLanguageItems.add("English");
        mLanguageItems.add("Spanish");
        mLanguageItems.add("Chinese");
        mLanguageItems.add("German");
        mLanguageItems.add("Portuguese");
        mLanguageItems.add("French");

        wheelChangeLanguage.setTextSize(30);
        wheelChangeLanguage.setItemsVisibleCount(5);
        wheelChangeLanguage.setAdapter(new ArrayWheelAdapter(mLanguageItems));
        wheelChangeLanguage.setTextColorCenter(Color.WHITE);
        wheelChangeLanguage.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                choose=index;
                ToastUtil.showToast(getContext(), "" + mLanguageItems.get(index));
            }
        });
    }
    @OnClick(R.id.btn_set_data)
    public void onViewClicked() {
        String language = null;
        language = LanguageType.ENGLISH.getLanguage();
        changeLanguage(language);
    }


    /**
     * 如果是7.0以下，我们需要调用changeAppLanguage方法，
     * 如果是7.0及以上系统，直接把我们想要切换的语言类型保存在SharedPreferences中即可
     * 然后重新启动MainActivity
     * @param language
     */
    private void changeLanguage(String language) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            LanguageUtil.changeAppLanguage(DelinApplication.getContext(), language);
        }
        SpUtil.getInstance(getContext()).putString(SpUtil.LANGUAGE, language);
        Intent intent = new Intent(getContext(), LaunchActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }
}
